/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils.code;
import javax.swing.text.*;
import java.awt.Color;
import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import utils.properties.YourJavaPropertiesUtils;

/**
 *
 * @author Juan Carlos García del Canto
 */
public class CodeUtils {
    
    
  /**
   * This method compare if the current word is a reserved word in the system and replace with the 
   * correct color and the correct format.
   * 
   * @param wrd The completly word that the system will analize
   * @param fileDetailsTextArea JTextPane to insert the new Code
   */
  
  public static void replaceCode(String wrd,JTextPane fileDetailsTextArea,int position,StyleContext sc,DefaultStyledDocument doc){

      
      try{
                
          Style red = sc.addStyle("ConstantWidth", null);
          StyleConstants.setForeground(red, Color.red);
          
          doc.setCharacterAttributes(position,wrd.length(),red, false);
        
          String currentDictionary = YourJavaPropertiesUtils.getProperty("currentDictionary","properties");
          StringTokenizer stDictionary = new StringTokenizer(currentDictionary,"-");
          String title = "";
          while(stDictionary.hasMoreTokens()) { 
              title = stDictionary.nextToken().trim();
              break;
          }
          FileInputStream fstream = new FileInputStream("src/resources/dictionaries/"+title+".yjd");

          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
                StringTokenizer st = new StringTokenizer(strLine, ":");
                while(st.hasMoreTokens()) {
                    String key = st.nextToken();
                    if(key.equals(wrd)){
                        String val = st.nextToken();
                        fileDetailsTextArea.setText(fileDetailsTextArea.getText().replaceAll(wrd,val)); 
                        int newPosition = (val.length() - wrd.length()) + position;
                        fileDetailsTextArea.setCaretPosition(newPosition);
                    }
                    
                    
                }

 
          }
      
      
      
      }catch(Exception e){
          
          
      } 
      
       
      
      
  }
  
   /**
   * This method compare if the current word is a reserved word in the system and replace with the 
   * correct color and the correct format.
   * 
   * @param wrd The completly word that the system will analize
   * @param fileDetailsTextArea JTextPane to insert the new Code
   * 
   * @return Returns a String with the new wordCode 
   * 
   */
  
  public static String replaceCode(String wrd){
      
      
      try{
          String currentDictionary = YourJavaPropertiesUtils.getProperty("currentDictionary","properties");
          StringTokenizer stDictionary = new StringTokenizer(currentDictionary,"-");
          String title = "";
          while(stDictionary.hasMoreTokens()) { 
              title = stDictionary.nextToken().trim();
              break;
          }
          FileInputStream fstream = new FileInputStream("src/resources/dictionaries/"+title+".yjd");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
                StringTokenizer st = new StringTokenizer(strLine, ":");
                
                while(st.hasMoreTokens()) {
                    String key = st.nextToken();
                    
                    if(key.equals(wrd)){
                        String val = st.nextToken();
                        return val;

                    }
                    
                    
                }
                
          }
          
      
          return wrd;
      
      }catch(Exception e){
         
          JOptionPane.showMessageDialog(null,e.getMessage());
      } 
      
       
      return wrd;
      
  }

  /**
   * This method replace all the code when the user opens a new file
   * 
   * @param file The File to encode
   * @return String with the final code to insert in the system
   */
  public static String replaceCodeOnOpen(File file){
      
      
      String newCode = "";
      
      try{
          
          FileInputStream fstream = new FileInputStream(file);

          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
              StringTokenizer st = new StringTokenizer(strLine, " ");
              while(st.hasMoreTokens()) { 
                  newCode+=" "+replaceCode(st.nextToken());
              }
              
              newCode+="\n";
              
          }
          
      }catch(Exception e){
          
          JOptionPane.showMessageDialog(null,e.getMessage());
      }
      
      return newCode;
      
  }
  
  /**
   * This method replace the current code with the Standar Java Language
   * to save the file correctly.
   * 
   * @param yourJavaCode
   * @return String with the Standar Code
   */
  public static String replaceCodeOnSave(String yourJavaCode){
      
      String codeToSave = "";
      int count = 0;
      
      try{
          String currentDictionary = YourJavaPropertiesUtils.getProperty("currentDictionary","properties");
          StringTokenizer stDictionary = new StringTokenizer(currentDictionary,"-");
          String title = "";
          while(stDictionary.hasMoreTokens()) { 
              title = stDictionary.nextToken().trim();
              break;
          }
          FileInputStream fstream = new FileInputStream("src/resources/dictionaries/"+title+".yjd");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine = "";
          while ((strLine = br.readLine()) != null)   {
              
                StringTokenizer st = new StringTokenizer(strLine, ":");
                
                while(st.hasMoreTokens()) {
                    String key = st.nextToken();
                    String val = st.nextToken();
                    if(!key.contains("#")){ // Ignore comments   
                        if(key != null && val != null){
                            if(count==0){
                                codeToSave=yourJavaCode.replaceAll(val,key); 
                                count++;
                            }else{

                                codeToSave=codeToSave.replaceAll(val,key); 

                            }

                        }
                    }
                }
            }
          
      
      }catch(Exception e){
         
          JOptionPane.showMessageDialog(null,"Error encoding file");
      } 
      
      return codeToSave;
  }
  
  /*(public static String replaceCodeWithDictionaries(){
      
  }*/
   
}
